package com.xiaobai.controller;

import com.xiaobai.config.OrderInformationConfig;
import com.xiaobai.feign.ProductFeignClient;
import com.xiaobai.order.bean.Order;
import com.xiaobai.product.bean.Product;
import com.xiaobai.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
//@RequestMapping("/api/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    private OrderInformationConfig orderInformationConfig;

    //    @GetMapping("/userInfo")
//    public String getUserInfo(){
//        return userName + ":" + userAge;
//    }
    @GetMapping("/orders")
    private Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId) {
        return orderService.getOrderById(productId, userId);
    }

    @GetMapping("/orderConfig")
    public String getOrderConfig() {
        return "time-out: " + orderInformationConfig.getTimeOut() + "\t dataBase : " + orderInformationConfig.getDataBase()
                + "\t autoConfig : " + orderInformationConfig.getAutoConfig();
    }

    //feign远程调用的结果
    @GetMapping("/getResult/{id}")
    public Product getResult(@PathVariable Long id) {
        return productFeignClient.getProductById(id);
    }

    @GetMapping("/create")
    private Order createOrderProduct(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId) {
        return orderService.getOrderByFeignId(productId, userId);
    }

    @GetMapping("/readDb")
    private String readDb() {
        log.info("readDb success...");
        return "readDb success...";
    }

    @GetMapping("/writeDb")
    private String writeDb() {
        log.info("writeDb success...");
        return "writeDb success...";
    }
}
